package com.tarena.lbs.activity.dao.repository.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.activity.dao.mapper.ActivityMapper;
import com.tarena.lbs.activity.dao.repository.ActivityRepository;
import com.tarena.lbs.activity.pojo.po.ActivityPO;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ActivityRepositoryImpl implements ActivityRepository {


    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public void add(ActivityPO activityPO) {
        log.debug("活动保存，入参:{}",activityPO);
        activityMapper.insert(activityPO);
    }

    @Override
    public PageInfo<ActivityPO> pagesList(ActivityQuery query) {
        QueryWrapper<ActivityPO> queryWrapper = assembleActivityQuery(query);
        //PageHelper.startPage 之间不能有代码
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<ActivityPO> pos = activityMapper.selectList(queryWrapper);
        //selectList 数据库操作 之间不能有代码
        return new PageInfo<>(pos);
    }

    @Override
    public ActivityPO findById(Integer id) {
        return activityMapper.selectById(id);
    }

    private QueryWrapper<ActivityPO> assembleActivityQuery(ActivityQuery query) {
        QueryWrapper<ActivityPO> queryWrapper = new QueryWrapper<>();
        //添加商家ID查询条件
        if (query.getBusinessId()!=null){
            queryWrapper.eq("business_id",query.getBusinessId());
        }
        if (StringUtils.isNotBlank(query.getActivityName())){
            queryWrapper.like("activity_name",query.getActivityName());
        }
        if (query.getDeliveryChannel()!=null){
            queryWrapper.eq("delivery_channel",query.getDeliveryChannel());
        }
        if (query.getType()!=null){
            queryWrapper.eq("type",query.getType());
        }
        if (query.getChannelType()!=null){
            queryWrapper.eq("channel_type",query.getChannelType());
        }
        if (query.getStatus()!=null){
            queryWrapper.eq("status",query.getStatus());
        }
        if (StringUtils.isNotBlank(query.getStartDate())){
            queryWrapper.ge("start_date",query.getStartDate());
        }
        if (StringUtils.isNotBlank(query.getEndDate())){
            queryWrapper.le("end_date",query.getEndDate());
        }
        return queryWrapper;
    }
}
